/**
 * Copyright &copy; 2013-2015 山东易科德软件有限公司 All rights reserved.
 */
package cn.net.ecode.common.utils;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.net.ecode.common.config.Global;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 * Cache工具类
 * @author ThinkGem
 * @version 2015-7-21
 */
public class CacheUtils {
	
	protected static Logger logger = LoggerFactory.getLogger(CacheUtils.class);
	private static CacheManager cacheManager = ((CacheManager)SpringContextHolder.getBean("cacheManager"));

	private static final String SYS_CACHE = "sysCache";

	/**
	 * 获取SYS_CACHE缓存
	 * @param key
	 * @return
	 */
	public static Object get(String key) {
		return get(SYS_CACHE, key);
	}
	
	/**
	 * 获取SYS_CACHE缓存
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public static Object get(String key, Object defaultValue) {
		Object value = get(key);
		return value != null ? value : defaultValue;
	}
	
	/**
	 * 写入SYS_CACHE缓存
	 * @param key
	 * @return
	 */
	public static void put(String key, Object value) {
		put(SYS_CACHE, key, value);
	}
	
	/**
	 * 从SYS_CACHE缓存中移除
	 * @param key
	 * @return
	 */
	public static void remove(String key) {
		remove(SYS_CACHE, key);
	}
	
	/**
	 * 获取缓存
	 * @param cacheName
	 * @param key
	 * @return
	 */
	public static Object get(String cacheName, String key) {
		Element element = getCache(cacheName).get(key);
		return element != null ? element.getObjectValue() : null;
	}
	
	/**
	 * 获取缓存
	 * @param cacheName
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public static Object get(String cacheName, String key, Object defaultValue) {
		Object value = get(cacheName, key);
		return value != null ? value : defaultValue;
	}
	
	/**
	 * 写入缓存
	 * @param cacheName
	 * @param key
	 * @param value
	 */
	public static void put(String cacheName, String key, Object value) {
		Element element = new Element(key, value);
		getCache(cacheName).put(element);
	}

	/**
	 * 从缓存中移除
	 * @param cacheName
	 * @param key
	 */
	public static void remove(String cacheName, String key) {
		getCache(cacheName).remove(key);
	}

	/**
	 * 从缓存中移除所有
	 * @param cacheName
	 */
	public static void removeAll(String cacheName) {
		getCache(cacheName).removeAll();
	}
	
	/**
	 * 获得一个Cache，没有则显示日志。
	 * @param cacheName
	 * @return
	 */
	private static Cache getCache(String cacheName){
		Cache cache = cacheManager.getCache(cacheName);
		if (cache == null){
//			cacheManager.addCache(cacheName);
//			cache = cacheManager.getCache(cacheName);
//			cache.getCacheConfiguration().setEternal(true);
			logger.warn("没有“"+cacheName+"”这个缓存，请在ehcache-local.xml中定义。");
		}
		return cache;
	}

	/**
	 * 获得Cache管理对象
	 */
	public static CacheManager getCacheManager() {
		return cacheManager;
	}
	
	/**
	 * 根据缓存名称清理缓存
	 */
	public static void clearCache(String cacheName) {
		Cache cache = getCache(cacheName);
		if (cache != null){
			@SuppressWarnings("unchecked")
			String keys = StringUtils.join((List<String>)cache.getKeys(), ",");
			logger.info("清理缓存 “{}” keys: {}", cacheName, keys);
			cache.removeAll();
		}
	}
	
	/**
	 * 清空平台缓存
	 */
	public static void clearPlatform() {
		String platform = Global.getConfig("cache.name.platform");
		if (StringUtils.isBlank(platform)) {
			return;
		}
		for (String cacheName : platform.split(",")) {
			clearCache(cacheName);
		}
	}

	/**
	 * 清空项目缓存
	 */
	public static void clearProject() {
		String project = Global.getConfig("cache.name.project");
		if (StringUtils.isBlank(project)) {
			return;
		}
		for (String cacheName : project.split(",")) {
			clearCache(cacheName);
		}
	}

}
